package com.lzj.web.controller.system;

import com.lzj.common.core.controller.BaseController;
import com.lzj.common.core.domain.R;
import com.lzj.common.core.domain.entity.SysMenu;
import com.lzj.common.core.domain.entity.SysUser;
import com.lzj.common.core.domain.model.LoginBody;
import com.lzj.common.core.domain.model.LoginUser;
import com.lzj.system.domain.vo.RouterVo;
import com.lzj.system.service.ISysMenuService;
import com.lzj.system.service.ISysUserService;
import com.lzj.system.service.SysLoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 登录接口
 * 
 * @author 贱贱
 */
@Validated
@RequiredArgsConstructor
@RestController
public class SysLoginController extends BaseController {

    private final SysLoginService loginService;
    private final ISysUserService userService;
    private final ISysMenuService menuService;

    /**
     * 登录
     * 
     * @param loginBody 登录表单
     * @return 认证信息
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody @Validated LoginBody loginBody) {
        String token = loginService.login(loginBody);
        return R.ok("登录成功", token);
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public Map<String, Object> getInfo() {
        LoginUser loginUser = getLoginUser();
        SysUser user = userService.selectUserById(loginUser.getUserId());
        HashMap<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roles", loginUser.getRoles());
        ajax.put("permissions", loginUser.getPermissions());
        return ajax;
    }

    /**
     * 获取路由信息
     * @return 路由嵌套列表
     */
    @GetMapping("/getRouters")
    public List<RouterVo> getRouters() {
        List<SysMenu> sysMenus = menuService.selectMenuTreeByUserId(getUserId());
        return menuService.buildMenus(sysMenus);
    }

    ///**
    // * 获取路由信息 (新)
    // * @return 路由嵌套列表
    // */
    //@GetMapping("/getMenus")
    //public List<RouterVo> getMenus(){
    //    List<SysMenu> sysMenus = menuService.selectMenuTreeByUserId(getUserId());
    //}

    
}
